import { CreateModalList } from '@/biz-components/hoc-form/with-list';
// import { WithNormalRender } from "@ey-render/hoc-form";
import { xhrPostJson } from '@/services';
import { onFieldValueChange } from '@formily/core';
import { Spin, message } from 'antd';
import { useRef, useState } from 'react';
import { history } from "umi";
import api, { theArchive } from './services';

const WithListComp = CreateModalList({});

const ArchivesManageList = (props: any) => {
  const [refresh, setRefresh] = useState(false);
  const [selectedRowKeys, setSelectedRowKeys] = useState<string[]>([]);
  const [loading, setLoading] = useState(false);
  const formRef = useRef();
  const { match } = props;

  const rowSelection = {
    selectedRowKeys,
    onChange: (rowKeys: string[], selectedRows: any) => {
      setSelectedRowKeys(rowKeys);
    },
  };

  return (
    <Spin spinning={loading}>
      <WithListComp
        match={match}
        history={history}
        reloadFlag={refresh}
        pageCode={'ArcvManage_WillArchive_L'}
        pageApi={api.page}
        rowKey={'code'}
        noModal
        noCache
        rowSelection={rowSelection}
        formConfigs={{
          effects: (form: any) => {
            formRef.current = form;
          },
        }}
        validator={{
          checkColBtns: (key, row) => {
            if (key === 'bianji') {
              // return ![
              //   "Form",
              //   "Attachments",
              //   "Invoices",
              //   "ElecApvlRec",
              //   "BankElecReceipt",
              //   "Vouchers"
              // ].includes(row.arcvType);
              return row.isManuallyAdded;
            }

            return true;
          }
        }}
        events={{
          buttonsEvents: {
            handleCreate: () => {
                history.push('/archives/archives-manage/create');
            },
            handleArchive: () => {
                if (selectedRowKeys.length === 0) {
                    message.error('请选择归档数据');
                    return;
                }
                setLoading(true);
                theArchive({
                    "arcvCodes": selectedRowKeys
                }).then((res: any) => {
                    if (res.success) {
                        message.success('归档成功');
                        setRefresh(!refresh);
                    }
                }).finally(() => {
                    setLoading(false);
                })
            }
          },
          tableButtonsEvents: {
            handleEdit: (key: any, row: any) => {
                history.push(`/archives/archives-manage/edit/${row.code}`);
            },
            handleView: (key: any, row: any) => {
                history.push(`/archives/archives-manage/detail/${row.code}/undefined/undefined`);
            },
          },
        }}
      />
    </Spin>
  );
};

export default ArchivesManageList;
